<?php

class StaticController extends Zend_Controller_Action
{
    protected $_config;
    protected $_db;

    public function init()
    {
        $this->_helper->Feeder->init();
        $bootstrap = $this->getInvokeArg('bootstrap');
        $this->_config = $bootstrap->getOptions();
    }
    
    public function indexAction()
    {
        
    }
    
    public function loginAction()
    {
        WZ_Utils::template(true);
        setvar('errorLogin', false);
        if (count($_POST)) {
            $username = getvarpost('username');
            $password = getvarpost('password');
            
            $dbAdapter = Zend_Registry::get('ZendDb');
            $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
            $authAdapter->setTableName('user');
            $authAdapter->setIdentityColumn('username');
            $authAdapter->setCredentialColumn('password');
            $authAdapter->setIdentity($username);
            $authAdapter->setCredential(md5($password));
            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);
             
            if ($result->isValid()) {
               $data = $authAdapter->getResultRowObject(null, 'password');
               $auth->getStorage()->write($data);
               $authUser = Zend_Auth::getInstance()->getStorage()->read();
               setoption('authUser', $authUser);
               go(WZ_Utils::urlizer('home'));
            } else {
                setvar('errorLogin', true);
            }
        }
    }
    
    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        go(WZ_Utils::urlizer('home'));
    }
    
    public function forgetLoginAction()
    {
        WZ_Utils::template(true);
        setvar('errorForgetLogin', false);
        setvar('successForgetLogin', false);
        if (count($_POST)) {
            $email = getvarpost('email');
            $user = new WZModel_User();
            $res = $user->fetchAll("email = '$email'");
            $nbRes = count($res);
            if (0 < $nbRes) {
                $findUser = $res->current();
                $newPassword = makePassword();
                $findUser->password = md5($newPassword);
                $findUser->save();
                $subject = __('Votre nouveau mot de passe');
                $from = 'ajf@zendpro.com';
                mel($email, $subject, $newPassword, $from);
                setvar('successForgetLogin', true);
            } else {
                setvar('errorForgetLogin', true);
            }
        }
        $this->render('login', 'tab-forget');
    }
    
    public function produitAction()
    {
        WZ_Utils::title('Test');
        WZ_Utils::template(true);
        $produit = $this->getRequest()->getParam('produit');
        var_dump($produit);exit;
    }
    
    public function homeAction()
    {
        WZ_Utils::title('Tableau de bord');
        WZ_Utils::template(true);
    }
    
    public function is404Action()
    {
        WZ_Utils::title('Page inconnue');
        WZ_Utils::template(true);
    }
    
    public function is500Action()
    {
        WZ_Utils::title('Erreur système');
        WZ_Utils::template(true);
    }
}